<script setup lang="ts">
//
import { ref } from 'vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import Skeleton from './components/Skeleton.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
// import type { MDGuessInstance } from '@/types/component'
import { useGuessList } from '@/composables/index.ts'
// 猜你喜欢组合式函数
const { guessRef, onScrolltolower } = useGuessList()

const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
// const Guess = ref<MDGuessInstance>()
const getHomeBannerDate = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  bannerList.value = res.result
}
const getHomeCategoryDate = async () => {
  const res = await getHomeCategoryAPI()
  console.log(res)
  categoryList.value = res.result
}
const getHomeHotDate = async () => {
  const res = await getHomeHotAPI()
  console.log(res)
  hotList.value = res.result
}
// 上拉加载更多
/**const onScrolltolower = () => {
     console.log('上拉加载更多')
     Guess.value?.getMore()
  }
*/
// 下拉刷新状态
const isTriggered = ref(false)
// 骨架屏的显示
const isLoading = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData()
  // 加载数据 Promise.all 同时执行这些请求
  await Promise.all([
    getHomeBannerDate(),
    getHomeCategoryDate(),
    getHomeHotDate(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}
onLoad(async () => {
  isLoading.value = true
  await Promise.all([
    getHomeBannerDate(),
    getHomeCategoryDate(),
    getHomeHotDate(),
    guessRef.value?.getMore(),
  ])
  isLoading.value = false
})
</script>

<template>
  <!-- 首页导航栏 -->
  <CustomNavbar />
  <!---->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
  >
    <Skeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮播图 -->
      <MDSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门面板 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <MDGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
/* 解决安卓端没有dispaly:flex;的问题*/
/* #ifdef APP-PLUS */
#app,
/* #endif */
page {
  /*#ifdef H5*/
  background-color: #f5f5f5;
  /*#endif*/
  /*#ifndef MP-WEIXIN*/
  background-color: #fff;
  /*#endif*/
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
